<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      show-checkbox
      default-expand-all
      check-strictly
      :data="list"
      :props="{label:'name'}"
    />
    <el-row type="flex" justify="end">
      <el-button size="mini">取消</el-button>
      <el-button size="mini" type="primary" @click="hSubmit">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTreeData } from '@/utils'
import { assignPermission, getRolesDetail } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 数据渲染
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        this.list = transDataToTreeData(res.data)
        const detail = await getRolesDetail(this.curId)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
      } catch (error) {
        console.log(error)
      }
    },
    // 点击确定实现分配角色
    hSubmit() {
      try {
        // 获取当前选中的树节点id
        const permIds = this.$refs.tree.getCheckedKeys()
        assignPermission({ id: this.curId, permIds: permIds })
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
